/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.impl;

import com.ivanbiz.dao.LabaRugiDAO;
import com.ivanbiz.model.BiayaLain;
import com.ivanbiz.model.HibernateUtil;
import com.ivanbiz.model.ItemNota;
import com.ivanbiz.model.ItemNotaLain;
import com.ivanbiz.model.ItemPembelian;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author User
 */
public class LabaRugiDAOImpl extends GenericDAOImpl implements LabaRugiDAO {

    public Map hitungTotalLabaRugi(Date start, Date end) throws Exception {
        Map map = new HashMap();
        try {
            Session session = HibernateUtil.getSession();
            start.setHours(0);
            start.setMinutes(0);
            start.setSeconds(0);
            end.setHours(0);
            end.setMinutes(0);
            end.setSeconds(0);
            Criteria crit = null;
            double hitung = 0;
            List listItem = new ArrayList();
            // start hitung item nota
            crit = session.createCriteria(ItemNota.class).add(Restrictions.ge("createDate", start)).add(Restrictions.le("createDate", end));
            listItem = crit.list();
            for (int x = 0; x < listItem.size(); x++) {
                ItemNota itemNota = (ItemNota) listItem.get(x);
                hitung += (itemNota.getHargaJual()-itemNota.getHargaModal())*itemNota.getJumlah();
                //hitung += itemNota.getHargaJual() * itemNota.getJumlah();
            }
            map.put("totalItemNota", hitung);
            map.put("listItemNota", listItem);
            hitung = 0;
            // end hitung item nota

            // start hitung item nota lain
            crit = session.createCriteria(ItemNotaLain.class).add(Restrictions.ge("createDate", start)).add(Restrictions.le("createDate", end));
            listItem = crit.list();
            for (int x = 0; x < listItem.size(); x++) {
                ItemNotaLain itemNotaLain = (ItemNotaLain) listItem.get(x);
                hitung += itemNotaLain.getHargaJual();
            }
            map.put("totalItemNotaLain", hitung);
            map.put("listItemNotaLain", listItem);
            hitung = 0;
            // end hitung item nota lain

            // start hitung pembelian
            crit = session.createCriteria(ItemPembelian.class).add(Restrictions.ge("createDate", start)).add(Restrictions.le("createDate", end));
            listItem = crit.list();
            for (int x = 0; x < listItem.size(); x++) {
                ItemPembelian itemPembelian = (ItemPembelian) listItem.get(x);
                hitung += itemPembelian.getHarga() * itemPembelian.getJumlah();
            }
            map.put("totalItemPembelian", hitung);
            map.put("listItemPembelian", listItem);
            hitung = 0;
            // end hitung pembelian

            // start biaya lain2
            crit = session.createCriteria(BiayaLain.class).add(Restrictions.ge("createDate", start)).add(Restrictions.le("createDate", end));
            listItem = crit.list();
            for (int x = 0; x < listItem.size(); x++) {
                BiayaLain biayaLain = (BiayaLain) listItem.get(x);
                hitung += biayaLain.getJumlahBiaya();
            }
            map.put("totalBiayaLain", hitung);
            map.put("listBiayaLain", listItem);
            hitung = 0;
            // end biaya lain2
            hitung = ((Double) map.get("totalItemNota") + (Double) map.get("totalItemNotaLain") - ((Double) map.get("totalItemPembelian") + (Double) map.get("totalBiayaLain")));

            map.put("totalAll", hitung);
            hitung = 0;

        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
        return map;
    }
}
